package airthmetic.exercise.stack;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @Author douhy
 * @description 最小栈算法Demo
 * @date 22/03/31
 * leetCode 155. 最小栈
 * 思路：用另个一个栈存最小及更小的数字
 */
public class MinStack {

    Stack<Integer> mainStack;
    Stack<Integer> minStack;

    public MinStack() {
        mainStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        mainStack.push(val);
        if (minStack.isEmpty() || val <= minStack.peek()) {
            minStack.push(val);
        }
    }

    public void pop() {
        if (mainStack.pop().equals(minStack.peek())) {
            minStack.pop();
        }
    }

    public int top() {
        return mainStack.peek();
    }

    public int getMin() {
        return minStack.peek().intValue();
    }

    @Override
    public String toString() {
        return "MinStack{" +
                "mainStack=" + mainStack +
                ", minStack=" + minStack +
                '}';
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());
        System.out.println(minStack);
        minStack.pop();
        System.out.println(minStack);
    }
}
